﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace miniGameCollection
{
    public class JumpGame : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private SpriteFont gamefont;
        //private Texture2D bomb;
        private Texture2D square;
        private int squareWidth;

        private Player player;

        private List<Platform> platforms;

        Random random = new Random(DateTime.Now.Millisecond);
        private static readonly int MinLength = 50;
        private static readonly int MaxLength = 80;

        int MaxDistance = 200;
        private MouseState lastMouseState;
        private int direction = 0;
        private int jumpDistance;
        private bool isJumping;
        private bool isGameOver;

        public JumpGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            this.IsMouseVisible = true;
            base.Initialize();
            StartGame();
        }

        private void StartGame()
        {
            platforms = new List<Platform>();
            player = new Player();
            player.Position = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height - 100);
            player.Texture = Content.Load<Texture2D>("bomb");

            Platform plat0 = new Platform();
            plat0.Shape = ShapeType.Square;
            plat0.Postion = player.Position;
            plat0.Length = random.Next(MinLength, MaxLength);
            plat0.Texture = new Texture2D(graphics.GraphicsDevice, plat0.Length, plat0.Length);
            plat0.Texture.CreateBorder(1, Color.Black);
            platforms.Add(plat0);

            Platform plat1 = CreateNewPlatform();
            platforms.Add(plat1);
        }

        private Platform CreateNewPlatform()
        {
            Platform plat = new Platform();
            plat.Shape = ShapeType.Square;
            plat.Length = random.Next(MinLength, MaxLength);

            direction = random.Next(-1, 2);
            int distance = random.Next(50, MaxDistance);
            Vector2 tmp = player.Position;
            Vector2 pos = ChangePosition(tmp, distance);
            plat.Postion = pos;

            plat.Texture = new Texture2D(graphics.GraphicsDevice, plat.Length, plat.Length);
            plat.Texture.CreateBorder(1, Color.Black);
            return plat;
            //platforms.Add(plat);
        }

        private Vector2 ChangePosition(Vector2 tmp, int distance)
        {
            Vector2 pos = new Vector2();
            if (direction == -1)
            {
                pos = new Vector2(tmp.X - distance, tmp.Y);
            }
            else if (direction == 0)
            {
                pos = new Vector2(tmp.X, tmp.Y - distance);
            }
            else if (direction == 1)
            {
                pos = new Vector2(tmp.X + distance, tmp.Y);
            }

            return pos;
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //square = new Texture2D(GraphicsDevice, squareWidth, squareWidth);

            //bomb = Content.Load<Texture2D>("bomb");

            gamefont = Content.Load<SpriteFont>("gamefont");
        }

        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyboard = Keyboard.GetState();
            if (keyboard.IsKeyDown(Keys.Enter))
            {
                StartGame();
                isGameOver = false;
            }

            MouseState mouseState = Mouse.GetState();//获取鼠标点击的状态
            MinePosition current = new MinePosition();
            if (lastMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released)
            {
                //Vector2 taget = ChangePosition(player.Position,jumpDistance);
                //jumpDistance = 0;

                JumpToPositon(gameTime);
            }
            else if(mouseState.LeftButton == ButtonState.Pressed)
            {
                jumpDistance = jumpDistance + 2;
            }
            lastMouseState = mouseState;
            if (isJumping)
            {
                JumpToPositon(gameTime);
            }

            base.Update(gameTime);
        }

        private void JumpToPositon(GameTime gameTime)
        {
            if (jumpDistance > 0)
            {
                isJumping = true;
                int delta = 5;
                jumpDistance = jumpDistance - delta;
                player.Position = ChangePosition(player.Position, delta);
            }
            else
            {
                jumpDistance = 0;
                isJumping = false;
                if (!CheckGameOver())
                {
                    EnterNewPlatform();
                }
            }
        }

        private bool CheckGameOver()
        {
            Platform lastPlat = platforms.Last();
            if (lastPlat.Shape == ShapeType.Square)
            {
                var distance = player.Position - lastPlat.Postion;
                if (Math.Abs(distance.X) <= lastPlat.Length && Math.Abs(distance.Y) <= lastPlat.Length)
                {
                    isGameOver = false;
                }
                else
                {
                    isGameOver = true;
                }
            }
            return isGameOver;
        }

        private void EnterNewPlatform()
        {
            Platform platNext = CreateNewPlatform();
            platforms.RemoveAt(0);
            platforms.Add(platNext);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            foreach (Platform plat in platforms)
            {
                Vector2 drawpos = new Vector2(plat.Postion.X - plat.Length / 2, plat.Postion.Y - plat.Length / 2);
                spriteBatch.Draw(plat.Texture,drawpos,plat.ForeColor); 
            }
            Vector2 playerPos = new Vector2(player.Position.X - player.Texture.Width / 2,      player.Position.Y -  player.Texture.Height / 2);
            spriteBatch.Draw(player.Texture,playerPos,Color.White);

            if (isGameOver)
            {
                string pressEnter = "Failed!Press Enter to restart";
                Vector2 pressEnterSize = gamefont.MeasureString(pressEnter);
                spriteBatch.DrawString(gamefont, pressEnter, new Vector2(graphics.GraphicsDevice.Viewport.Width / 2 - pressEnterSize.X / 2, graphics.GraphicsDevice.Viewport.Height - 200), Color.White);
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
